﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit.Electric.Common;
using HYElectricKernel;

namespace HYRElectric.WirePlace
{
	public class EquipmentConnectWires
	{
		public static void SelectEquipments(UIDocument uiDoc)
		{
			Document document = uiDoc.ActiveView.Document;
			List<Element> list = uiDoc.Selection.PickElementsByRectangle(new SelectionFilter(), "请选择要连线的设备").ToList<Element>();
			if (list.Count < 2)
			{
				YJKMessageBox.Show("框选的实例少于2个，请重新框选");
				return;
			}
			EquipmentConnectWires.CreatWireByElementList(list, document);
		}

		public static void CreatWireByElementList(List<Element> elementList, Document doc)
		{
			List<Element> list = new List<Element>();
			List<Element> list2 = new List<Element>();
			Dictionary<List<Element>, EdgeInfo> dictionary = new Dictionary<List<Element>, EdgeInfo>();
			list = EquipmentConnectWires.GetSortElements(doc, elementList);
			foreach (Element item in list)
			{
				list2.Add(item);
			}
			Element element = list[0];
			while (list.Count != 1)
			{
				XYZ origin = RevitUtils.GetDeviceConnector(element).Origin;
				list.Remove(element);
				list.Sort(new ElementToBasisPointComparer(origin));
				EquipmentEdgeLine equipmentEdgeLine = new EquipmentEdgeLine(doc, element, list[0], WireConnectForm.wireParams);
				EdgeInfo value = null;
				equipmentEdgeLine.GetDeviceEdgePoints(ref value, null);
				dictionary.Add(new List<Element>
				{
					element,
					list[0]
				}, value);
				element = list[0];
			}
			Transaction transaction = new Transaction(doc, "ConnectWire");
			try
			{
				transaction.Start();
				foreach (KeyValuePair<List<Element>, EdgeInfo> keyValuePair in dictionary)
				{
					NewConnectWire.CreateWireBySelected(doc, keyValuePair.Key, keyValuePair.Value);
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
			}
		}

		private static List<Element> GetSortElements(Document doc, List<Element> selElements)
		{
			List<XYZ> list = new List<XYZ>();
			foreach (Element element in selElements)
			{
				LocationPoint locationPoint = element.Location as LocationPoint;
				list.Add(locationPoint.Point);
			}
			list.Sort(new X_CoordinateComparer());
			double x = list[0].X;
			list.Sort(new Y_CoordinateComparer());
			double y = list[list.Count - 1].Y;
			selElements.Sort(new ElementToBasisPointComparer(new XYZ(x, y, 0.0)));
			return selElements;
		}

		private static void DeleteDuplicateConnections(Document doc, List<Element> selElements)
		{
			foreach (Element element in RevitFilter.GetElementsOfType(doc, doc.ActiveView.Id, typeof(Wire)).ToList<Element>())
			{
				Wire wire = element as Wire;
				Element element2;
				Element element3;
				RevitUtils.GetWireConnectedEquipment(wire, out element2, out element3);
				if (element2 != null && element3 != null)
				{
					EquipmentComparer comparer = new EquipmentComparer();
					if (selElements.Contains(element2, comparer) && selElements.Contains(element3, comparer))
					{
						doc.Delete(wire.Id);
					}
				}
			}
		}
	}
}
